ARG IMAGE

FROM $IMAGE

ARG name=redis \
    doc_commit=3fdb6df44ecd5c4d99ea52a0133177f5ebc24805

RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirror.nju.edu.cn|g' \
    -i /etc/yum.repos.d/CentOS-*.repo \
 && yum install -y epel-release \
 && sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!//download\.fedoraproject\.org/pub!//mirror.nju.edu.cn!g' \
    -e 's!//download\.example/pub!//mirror.nju.edu.cn!g' \
    -e 's!http://mirrors!https://mirrors!g' \
    -i /etc/yum.repos.d/epel*.repo

RUN yum -y install --setopt=tsflags=nodocs make gcc systemd-devel rpm-build rpmdevtools wget openssl-devel

ADD sources /sources
ADD redis.spec /redis.spec

ARG VERSION
ENV version=$VERSION

RUN rpmdev-setuptree \
 && cp /sources/* ~/rpmbuild/SOURCES/ \
 && short_doc_commit=$(c=${doc_commit}; echo ${c:0:7}) \
 && wget --no-check-certificate http://download.redis.io/releases/redis-${version}.tar.gz -O ~/rpmbuild/SOURCES/redis-${version}.tar.gz \
 && wget --no-check-certificate https://ghproxy.com/https://github.com/${name}/${name}-doc/archive/${doc_commit}/${name}-doc-${short_doc_commit}.tar.gz -O ~/rpmbuild/SOURCES/${name}-doc-${short_doc_commit}.tar.gz

RUN cat /redis.spec | sed -e "s#^Version:.*#Version:           ${version}#g" > ~/rpmbuild/SPECS/redis.spec \
 && cat  ~/rpmbuild/SPECS/redis.spec \
 && rpmbuild -bb ~/rpmbuild/SPECS/redis.spec

WORKDIR /root/rpmbuild/RPMS/x86_64
